<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>2021.11.08今天的作业</title>
	</head>
	<body>
		<h1>GET</h1>
		<button type="button" onclick="getTime()">获取当前日期和时间</button>
		<br />
		<button type="button" onclick="getPome()">随机一句名言</button>
		<br />
		<button type="button" onclick="getAuthor('')">获取诗词列表-包含作者信息</button>
		<br />
		<button type="button" onclick="getPoetry()">获取诗词作者列表</button>
		<br />
		<button type="button" onclick="getList()">平台接口预览</button>
		<br />

		<h1>POST</h1>
		<button onclick="getByPost('https://api.apiopen.top/getWangYiNews','1','1','1')">获取网易新闻</button>
		<br />
		<button onclick="getByPost('https://api.apiopen.top/getImages','1','1','1')">美图推荐</button>
		<br />
		<button onclick="getByPost('https://api.apiopen.top/likePoetry','李白','1','2')">模糊搜索古诗词-李白</button>
		<br />
		<button onclick="getByPost('https://api.apiopen.top/searchPoetry','古风二首 二','1','2')">获取-古风二首 二</button>
		<br />
		<button onclick="getByPost('https://api.apiopen.top/searchAuthors','李白','1','2')">搜索古诗词作者-李白</button>
		<br />
		<div id="maxdiv">
			你所要查看的内容：
		</div>
	</body>
	<script type="text/javascript">
		// https://api.apiopen.top/api.html#760bca74e8164227bae98f09c3079efe
		// 第一个：
		function getTime() {
			var ajax = new XMLHttpRequest();
			ajax.open('GET', 'http://poetry.apiopen.top/getTime');
			ajax.send();
			ajax.onreadystatechange = function() {
				if (ajax.status == 200 && ajax.readyState == 4) {
					var obj1 = JSON.parse(ajax.responseText);
					var maxdiv = document.getElementById('maxdiv');
					console.log(obj1.result)
					maxdiv.innerText = obj1.result.dateTime;
				}
			}
		}
		// 第二个：
		function getPome() {
			var ajax = new XMLHttpRequest();
			ajax.open('GET', 'http://poetry.apiopen.top/sentences');
			ajax.send();
			ajax.onreadystatechange = function() {
				if (ajax.status == 200 && ajax.readyState == 4) {
					var obj2 = JSON.parse(ajax.responseText);
					var maxdiv = document.getElementById('maxdiv');
					console.log(obj2.result)
					var sum = obj2.result.name + '——' + obj2.result.from
					maxdiv.innerText = sum;
				}
			}
		}
		// 第三个：
		function getAuthor() {
			var ajax = new XMLHttpRequest();
			ajax.open('GET', 'http://poetry.apiopen.top/poetryFull?count=2&page=1');
			ajax.send();
			ajax.onreadystatechange = function() {
				if (ajax.status == 200 && ajax.readyState == 4) {
					var obj3 = JSON.parse(ajax.responseText);
					var sum = '';
					for (var i = 0; i < obj3.result.length; i++) {
						sum = sum + '<h3>' + obj3.result[i].title + '</h3>' + obj3.result[i].simpleIntro + '<br />';
					}
					console.log(sum);
					var maxdiv = document.getElementById('maxdiv');
					console.log(obj3.result)
					maxdiv.innerHTML = sum;
				}
			}
		}
		// 第四个：
		function getPoetry() {
			var ajax = new XMLHttpRequest();
			ajax.open('GET', 'http://poetry.apiopen.top/poetry?count=2&page=1');
			ajax.send();
			ajax.onreadystatechange = function() {
				if (ajax.status == 200 && ajax.readyState == 4) {
					var obj4 = JSON.parse(ajax.responseText);
					var sum = '';
					for (var i = 0; i < obj4.length; i++) {
						sum = sum + obj4.result[i].title + '<br />';
						console.log(sum);
					}
					var maxdiv = document.getElementById('maxdiv');
					console.log(obj4);
					maxdiv.innerHTML = sum;
				}
			}
		}
		// 第五个：
		function getList() {
			var ajax = new XMLHttpRequest();
			ajax.open('GET', 'https://api.apiopen.top/getAllUrl');
			ajax.send();
			ajax.onreadystatechange = function() {
				if (ajax.status == 200 && ajax.readyState == 4) {
					var obj5 = JSON.parse(ajax.responseText);
					var sum = '';
					for (var i = 0; i < obj5.length - 1; i++) {
						sum = sum + obj5[i] + '<br />';
					}
					var maxdiv = document.getElementById('maxdiv');
					console.log(obj5);
					maxdiv.innerHTML = sum;
				}
			}
		}
		// POST
		function getByPost(url, par1, par2, choose) {
			var req = new XMLHttpRequest();
			req.open("post", url);
			if (choose == "1") {
				req.send("page=" + par1 + "&count=" + par2);
			} else if (choose == "2") {
				req.send("name=" + par1);
			}
			req.onreadystatechange = function() {
				if (req.readyState == 4 && req.status == 200) {
					console.log(req.responseText);
				}

			}
		}
		// onload = function(){
		// 	getTime();
		// }
	</script>
</html>
